package cn.kgc.eduservice.service.impl;

import cn.kgc.eduservice.entity.EduSubject;
import cn.kgc.eduservice.entity.excel.SubjectData;
import cn.kgc.eduservice.entity.subject.OneSubject;
import cn.kgc.eduservice.entity.subject.TwoSubject;
import cn.kgc.eduservice.listener.SubjectExcelListener;
import cn.kgc.eduservice.mapper.EduSubjectMapper;
import cn.kgc.eduservice.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
        InputStream in = file.getInputStream();
        EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //课程分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类
        QueryWrapper<EduSubject>wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类
        QueryWrapper<EduSubject>wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合， 用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i <oneSubjectList.size() ; i++) {
            //得到oneSubjectList中的每个对象
            EduSubject eduSubject = oneSubjectList.get(i);
            //把eduSubject对象中的值取出来放到OneSubject中
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            //把OneSubject对象放到finalSubjectList集合里
            finalSubjectList.add(oneSubject);
        //封装二级分类
            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int m = 0; m <twoSubjectList.size() ; m++) {
                //得到 twoSubjectList 中的每个对象
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类parentid 和一级分类id是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);

                }
            }
            //把二级分类放到一级分类里
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }
}
